package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgUserTemplateMessageSettingMapper;
import com.ygqh.baby.po.YgUserTemplateMessageSetting;
import com.ygqh.baby.po.YgUserTemplateMessageSettingExample;
import com.ygqh.baby.service.YgUserTemplateMessageSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class YgUserTemplateMessageSettingServiceImpl implements YgUserTemplateMessageSettingService{
	
	@Autowired
	private YgUserTemplateMessageSettingMapper ygUserTemplateMessageSettingMapper;
	
	@Override
	public List<YgUserTemplateMessageSetting> find() {
		YgUserTemplateMessageSettingExample example = new YgUserTemplateMessageSettingExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserTemplateMessageSettingMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgUserTemplateMessageSettingExample example = new YgUserTemplateMessageSettingExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserTemplateMessageSettingMapper.countByExample(example);
	}

	@Override
	public YgUserTemplateMessageSetting findById(Long id) {
		return ygUserTemplateMessageSettingMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgUserTemplateMessageSetting ygUserTemplateMessageSetting) {
		return ygUserTemplateMessageSettingMapper.insertSelective(ygUserTemplateMessageSetting);
	}

	@Override
	public int update(YgUserTemplateMessageSetting ygUserTemplateMessageSetting) {
		return ygUserTemplateMessageSettingMapper.updateByPrimaryKeySelective(ygUserTemplateMessageSetting);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgUserTemplateMessageSettingExample example = new YgUserTemplateMessageSettingExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgUserTemplateMessageSetting record = new YgUserTemplateMessageSetting();
		record.setStatus(DataStatus.Delete.name());
		return ygUserTemplateMessageSettingMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgUserTemplateMessageSetting ygUserTemplateMessageSetting) {
		int r;
		if (ygUserTemplateMessageSetting.getId() != null) {
			r = this.update(ygUserTemplateMessageSetting);
		} else {
			r = this.save(ygUserTemplateMessageSetting);
		}
		return r;
	}

	@Override
	public ResultSet<YgUserTemplateMessageSetting> search(QueryInfo queryInfo, String q) {
		int count = ygUserTemplateMessageSettingMapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgUserTemplateMessageSetting> list = ygUserTemplateMessageSettingMapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count,list);
	}

	@Override
	public boolean sendAble(Long userId, String templateId, String platformNo, AppType appType) {
		YgUserTemplateMessageSettingExample example = new YgUserTemplateMessageSettingExample();
		example.createCriteria().andAppTypeEqualTo(appType.name()).andPlatformNoEqualTo(platformNo)
			.andUserIdEqualTo(userId).andMessageIdEqualTo(templateId);
		List<YgUserTemplateMessageSetting> list = ygUserTemplateMessageSettingMapper.selectByExample(example);
		return CollectionUtils.isEmpty(list) || DataStatus.Valid.name().equals(list.get(0).getStatus());
	}

	@Override
	public List<YgUserTemplateMessageSetting> findListByUserId(Long userId, String platformNo, AppType appType) {
		YgUserTemplateMessageSettingExample example = new YgUserTemplateMessageSettingExample();
		example.createCriteria().andUserIdEqualTo(userId).andPlatformNoEqualTo(platformNo)
				.andAppTypeEqualTo(appType.name())
				.andStatusIn(Arrays.asList(DataStatus.Valid.name(), DataStatus.Disable.name()));
		return ygUserTemplateMessageSettingMapper.selectByExample(example);
	}
}

